﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using PublisherManagementSystem.Data;
using PublisherManagementSystem.Models;

namespace PublisherManagementSystem.Controllers
{
    public class PublisherStaffsController : Controller
    {
        private readonly PublisherManagementSystemContext _context;

        public PublisherStaffsController(PublisherManagementSystemContext context)
        {
            _context = context;
        }

        // GET: PublisherStaffs
        public async Task<IActionResult> Index()
        {
            var publisherManagementSystemContext = _context.PublisherStaff.Include(p => p.Publisher);
            return View(await publisherManagementSystemContext.ToListAsync());
        }

        // GET: PublisherStaffs/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var publisherStaff = await _context.PublisherStaff
                .Include(p => p.Publisher)
                .FirstOrDefaultAsync(m => m.StaffID == id);
            if (publisherStaff == null)
            {
                return NotFound();
            }

            return View(publisherStaff);
        }

        // GET: PublisherStaffs/Create
        public IActionResult Create()
        {
            ViewData["PublisherID"] = new SelectList(_context.Publisher, "PublisherID", "Address");
            return View();
        }

        // POST: PublisherStaffs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("StaffID,PublisherID,Name,Gender,BirthDate,Phone,Email,Position,JoinDate")] PublisherStaff publisherStaff)
        {
            if (ModelState.IsValid)
            {
                _context.Add(publisherStaff);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["PublisherID"] = new SelectList(_context.Publisher, "PublisherID", "Address", publisherStaff.PublisherID);
            return View(publisherStaff);
        }

        // GET: PublisherStaffs/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var publisherStaff = await _context.PublisherStaff.FindAsync(id);
            if (publisherStaff == null)
            {
                return NotFound();
            }
            ViewData["PublisherID"] = new SelectList(_context.Publisher, "PublisherID", "Address", publisherStaff.PublisherID);
            return View(publisherStaff);
        }

        // POST: PublisherStaffs/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("StaffID,PublisherID,Name,Gender,BirthDate,Phone,Email,Position,JoinDate")] PublisherStaff publisherStaff)
        {
            if (id != publisherStaff.StaffID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(publisherStaff);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PublisherStaffExists(publisherStaff.StaffID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["PublisherID"] = new SelectList(_context.Publisher, "PublisherID", "Address", publisherStaff.PublisherID);
            return View(publisherStaff);
        }

        // GET: PublisherStaffs/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var publisherStaff = await _context.PublisherStaff
                .Include(p => p.Publisher)
                .FirstOrDefaultAsync(m => m.StaffID == id);
            if (publisherStaff == null)
            {
                return NotFound();
            }

            return View(publisherStaff);
        }

        // POST: PublisherStaffs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var publisherStaff = await _context.PublisherStaff.FindAsync(id);
            if (publisherStaff != null)
            {
                _context.PublisherStaff.Remove(publisherStaff);
            }

            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool PublisherStaffExists(int id)
        {
            return _context.PublisherStaff.Any(e => e.StaffID == id);
        }
    }
}
